package io.itit.ecp.admin.server.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.server.entity.EnergyForecastDataEntity;
import io.itit.ecp.admin.server.mapper.EnergyForecastDataMapper;
import io.itit.ecp.admin.server.service.IEnergyForecastDataService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@Service
public class EnergyForecastDataServiceImpl extends JoinServiceImpl<EnergyForecastDataMapper, EnergyForecastDataEntity>
        implements IEnergyForecastDataService {

    @Override
    public BigDecimal sumForecastPrecision(String sceneProjectId, String sceneCompanyId, String sceneEdificeId,
                                           String sceneFloorId, String sceneRoomId, List<String> staticsTypes,
                                           LocalDateTime startTime, LocalDateTime endTime) {
        BigDecimal value = Joins.of(EnergyForecastDataEntity.class)
                .selectAs(builder -> builder.add("SUM(forecast_precision)", "forecast_precision", false))
                .eq(StringUtils.isNotEmpty(sceneProjectId), EnergyForecastDataEntity::getSceneProjectId, sceneProjectId)
                .eq(StringUtils.isNotEmpty(sceneCompanyId), EnergyForecastDataEntity::getSceneCompanyId, sceneCompanyId)
                .eq(StringUtils.isNotEmpty(sceneEdificeId), EnergyForecastDataEntity::getSceneEdificeId, sceneEdificeId)
                .eq(StringUtils.isNotEmpty(sceneFloorId), EnergyForecastDataEntity::getSceneFloorId, sceneFloorId)
                .eq(StringUtils.isNotEmpty(sceneRoomId), EnergyForecastDataEntity::getSceneRoomId, sceneRoomId)
                .in(CollectionUtil.isNotEmpty(staticsTypes), EnergyForecastDataEntity::getStaticsType, staticsTypes)
                .ge(Objects.nonNull(startTime), EnergyForecastDataEntity::getSolidifyDate, startTime)
                .le(Objects.nonNull(endTime), EnergyForecastDataEntity::getSolidifyDate, endTime)
                .joinGetOne(BigDecimal.class);
        return ObjectUtils.defaultIfNull(value, BigDecimal.ZERO).setScale(2, RoundingMode.HALF_DOWN);
    }

    @Override
    public List<EnergyForecastDataEntity> queryForList(String sceneProjectId, String sceneCompanyId,
                                                                  String sceneEdificeId, String sceneFloorId, String sceneRoomId,
                                                                  List<String> staticsTypes, LocalDateTime startTime, LocalDateTime endTime) {
        return lambdaQuery()
                .eq(StringUtils.isNotEmpty(sceneProjectId), EnergyForecastDataEntity::getSceneProjectId, sceneProjectId)
                .eq(StringUtils.isNotEmpty(sceneCompanyId), EnergyForecastDataEntity::getSceneCompanyId, sceneCompanyId)
                .eq(StringUtils.isNotEmpty(sceneEdificeId), EnergyForecastDataEntity::getSceneEdificeId, sceneEdificeId)
                .eq(StringUtils.isNotEmpty(sceneFloorId), EnergyForecastDataEntity::getSceneFloorId, sceneFloorId)
                .eq(StringUtils.isNotEmpty(sceneRoomId), EnergyForecastDataEntity::getSceneRoomId, sceneRoomId)
                .in(CollectionUtil.isNotEmpty(staticsTypes), EnergyForecastDataEntity::getStaticsType, staticsTypes)
                .ge(Objects.nonNull(startTime), EnergyForecastDataEntity::getSolidifyDate, startTime)
                .le(Objects.nonNull(endTime), EnergyForecastDataEntity::getSolidifyDate, endTime)
                .list();


    }
}
